package controllers

import (
	"github.com/astaxie/beego"
	"analysis-ss/dao"
	"analysis-ss/models"
	"analysis-ss/untils"
)

type PageController struct {
	beego.Controller
}

// @router / [get]
func (c *PageController) Get() {
	url :=beego.AppConfig.String("dbUrl")
	beego.Info("URL ",url)
	v := c.GetSession("loginStatus")
	if v == nil {
		c.Layout = "layout/layout.tpl"
		c.Data["PageTitle"] = "备课成绩查询系统"
		c.TplName = "login.tpl"
	} else {
		c.Data["Center"] = centerList("", 1, 999)
		c.Layout = "layout/layout.tpl"
		c.Data["PageTitle"] = "中心（老师）列表"
		c.TplName = "center.tpl"
	}
}

// 登录
// @router /center [get]
// @router /center [post]
func (c *PageController) Center() {
	v := c.GetSession("loginStatus")
	if v == nil {
		username := c.GetString("username")
		password := c.GetString("password")
		if username == "admin" && password == "admin" {
			c.Data["Center"] = centerList("", 1, 999)
			c.SetSession("loginStatus", int(1))
			beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600
			c.Data["PageTitle"] = "中心（老师）列表"
			c.Layout = "layout/layout.tpl"
			c.TplName = "center.tpl"
		} else {
			c.Data["err"] = "账号或者密码错误"
			c.Data["PageTitle"] = "备课成绩查询系统"
			c.Layout = "layout/layout.tpl"
			c.TplName = "login.tpl"
		}
	} else {
		c.Data["Center"] = centerList("", 1, 999)
		c.Layout = "layout/layout.tpl"
		c.Data["PageTitle"] = "中心（老师）列表"
		c.TplName = "center.tpl"
	}
}

// @router /Students [get]
func (c *PageController) Students() {
	v := c.GetSession("loginStatus")
	if v == nil {
		c.Data["err"] = "请重新登录"
		c.Data["PageTitle"] = "备课成绩查询系统"
		c.Layout = "layout/layout.tpl"
		c.TplName = "login.tpl"
	} else {
	centerId := c.GetString("centerId")
	teacherId := c.GetString("teacherId")
	centerName, _ := getCenterName(teacherId, centerId)
	c.Data["centerName"] = centerName
	c.Data["centerId"] = centerId
	c.Data["teacherId"] = teacherId
	c.Data["PageTitle"] = "学生列表"
	c.Layout = "layout/layout.tpl"
	c.TplName = "students.tpl"
	}
}

// @router /Course [get]
func (c *PageController) Course() {
	v := c.GetSession("loginStatus")
	if v == nil {
		c.Data["err"] = "请重新登录"
		c.Data["PageTitle"] = "备课成绩查询系统"
		c.Layout = "layout/layout.tpl"
		c.TplName = "login.tpl"
	} else {
	childId := c.GetString("childId")
	beego.Info(childId)
	c.Data["child"] = getChild(childId)
	c.Data["codeNames"] = codeList(childId)
	c.Data["record"] = childRecord(childId)
	c.Layout = "layout/layout.tpl"
	c.Data["PageTitle"] = "学生成绩列表"
	c.TplName = "course.tpl"
	}
}

func getCenterName(teacherId, centerId interface{}) (string, error) {
	var centerName string
	var err error
	if teacherId != "" {
		centerName, err = dao.QueryTeacherCentername(teacherId)
		if err != nil {
			beego.Debug(err)
			return "", err
		} else {
			return centerName, err
		}
	} else {
		centerName, err = dao.QueryCentername(centerId)
		if err != nil {
			beego.Debug(err)
			return "", err
		} else {
			return centerName, err
		}
	}
}

// 查看孩子的测评
func childRecord(childId interface{}) models.StudyAbilityReport {
	//	beego.Info(childId)
	//	record, err := dao.QueryChildRecord(childId)
	//	if err != nil {
	//		beego.Debug(err)
	//	}
	//	beego.Info("Record:", record)
	//	tingJueJiYiGuangDu := record.TingJueJiYiGuangDu
	//	tingJueChangShiJiYi := record.TingJueChangShiJiYi
	//	yueDuLiJieZhengQueLv := record.YueDuLiJieZhengQueLv
	//	//听记忆广度
	//	if tingJueJiYiGuangDu != "" {
	//		record.TingJueJiYiGuangDu = fmt.Sprint(tingJueJiYiGuangDu, "岁")
	//	}
	//	//听觉记忆时长
	//	if tingJueChangShiJiYi != "" {
	//		record.TingJueChangShiJiYi = fmt.Sprint(tingJueChangShiJiYi, "岁")
	//	}
	//	//阅读正确率
	//	if yueDuLiJieZhengQueLv != "" {
	//		record.YueDuLiJieZhengQueLv = fmt.Sprint(yueDuLiJieZhengQueLv, "%")
	//	}
	//
	var record models.StudyAbilityReport
	return record
}

// 获得孩子所有做过题目的项目
func codeList(childId interface{}) []models.ChildAbilityFeedbackConfig {
	codeNames, err := dao.QueryChildCode(childId)
	if err != nil {
		beego.Debug(err)
	}
	return codeNames
}

// 获得孩子的信息
func getChild(childId interface{}) models.Child {
	child, err := dao.QueryChild(childId)
	if err != nil {
		beego.Debug(err)
	}
	child.Age = untils.GetChildAge(child.Year, child.Month, child.Day)
	child.Grade = untils.GetchildGrade(child.Grade)
	return child
}
